home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.2 Applications 1996 May / SGI IRIX 6.2 Applications 1996 May.iso / dist / impr_dev.idb / usr / impressario / src / examples / libimp / impshow.c.z / impshow.c
Encoding:
C/C++ Source or Header  |  1996-05-07  |  8.4 KB  |  333 lines

  1. /**************************************************************************
  2.  *
  3.  *           Copyright (c)    1993 Silicon Graphics, Inc.
  4.  *            All Rights Reserved
  5.  *
  6.  *       THIS    IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI
  7.  *
  8.  * The copyright notice above does not evidence any actual of intended
  9.  * publication of such source code, and is an unpublished work by Silicon
  10.  * Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is
  11.  * the property of Silicon Graphics, Inc. Any use, duplication or
  12.  * disclosure not specifically authorized by Silicon Graphics is strictly
  13.  * prohibited.
  14.  *
  15.  * RESTRICTED RIGHTS LEGEND:
  16.  *
  17.  * Use, duplication or disclosure by the Government is subject to
  18.  * restrictions as set forth in subdivision (c)(1)(ii) of the Rights in
  19.  * Technical Data and Computer Software clause at DFARS 52.227-7013,
  20.  * and/or in similar or successor clauses in the FAR, DOD or NASA FAR
  21.  * Supplement. Unpublished - rights reserved under the Copyright Laws of
  22.  * the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.
  23.  * Shoreline Blvd., Mountain View, CA 94039-7311
  24.  **************************************************************************
  25.  *
  26.  * File: impshow.c
  27.  *
  28.  * Description: Displays an SGI Image file on the screen. This is a
  29.  *    simplified display program in that it requires RGB mode be
  30.  *    available.
  31.  *
  32.  *    Usage: impshow [-o offset] [-c cacheMode] image
  33.  *
  34.  *    Where:
  35.  *
  36.  *    -o    Specifies an offset into the file where the SGI image
  37.  *        starts. Default is 0.
  38.  *
  39.  *    -c    Specifies what form of image caching is to be used.
  40.  *        cacheMode is one of none, heap, map for no caching,
  41.  *        caching on the heap or memory mapped caching respectively.
  42.  *        Default is none.
  43.  *
  44.  **************************************************************************/
  45.  
  46.  
  47. #ident "$Revision: 1.2 $"
  48.  
  49.  
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <sys/types.h>
  54. #include <gl/gl.h>
  55. #include <gl/device.h>
  56. #include "imp.h"
  57.  
  58.  
  59. /* Local functions */
  60.  
  61. static void usage(char *progName);
  62. static int drawImage(char *progName, IMPImage *image);
  63.  
  64.  
  65. /**************************************************************************
  66.  *
  67.  * Function: main
  68.  *
  69.  * Description: Program entry point.
  70.  *
  71.  * Parameters: 
  72.  *    argc (I) - number of command line arguments
  73.  *    argv (I) - command line arguments
  74.  *
  75.  * Return: 0 if no errors. 1 if errors.
  76.  *
  77.  **************************************************************************/
  78.  
  79. int main(int argc, char *argv[])
  80. {
  81.     short val;
  82.     char *pname;
  83.     IMPImage *image;
  84.     int ch;
  85.     off_t offset = 0;
  86.     IMPCacheMode cache = IMPNoCache;
  87.  
  88.     /*
  89.      * Extract the program basename
  90.      */
  91.     pname = ((pname = strrchr(argv[0], '/')) == NULL) ? argv[0]: pname + 1;
  92.  
  93.     /*
  94.      * Determine if this machine can run 24 bit RGB
  95.      */
  96.     if (getgdesc(GD_BITS_NORM_SNG_RED) < 8) {
  97.     fprintf(stderr, "%s: 24-bit graphics system required\n", pname);
  98.     exit(1);
  99.     }
  100.  
  101.     /*
  102.      * Handle command line options
  103.      */
  104.     while ((ch = getopt(argc, argv, "o:c:")) != EOF) {
  105.         switch (ch) {
  106.         case 'o':
  107.         offset = atoi(optarg);
  108.         break;
  109.             case 'c':
  110.                 if (!strcasecmp(optarg, "none"))
  111.                     cache = IMPNoCache;
  112.                 else if (!strcasecmp(optarg, "heap"))
  113.                     cache = IMPHeapCache;
  114.                 else if (!strcasecmp(optarg, "map"))
  115.                     cache = IMPMapCache;
  116.                 else
  117.                     fprintf(stderr,
  118.                         "%s: unrecognized cache mode %s, none used\n",
  119.                         pname, optarg);
  120.                 break;
  121.             case '?':
  122.             default:
  123.                 usage(pname);
  124.                 exit(1);
  125.         }
  126.     }
  127.  
  128.     /*
  129.      * Check the argument count
  130.      */
  131.     if (argc - optind < 1) {
  132.     usage(pname);
  133.     exit(1);
  134.     } 
  135.  
  136.     /*
  137.      * Open the iamge
  138.      */
  139.     if ((image = impOpenExt(argv[optind], "r", offset, cache)) == NULL ) {
  140.     impPerror(pname);
  141.     exit(1);
  142.     }
  143.  
  144.     /*
  145.      * We cannot accomodate images larger than the screen
  146.      */
  147.     if (impXSize(image) > getgdesc(GD_XPMAX) ||
  148.                 impYSize(image) > getgdesc(GD_YPMAX)) {
  149.     fprintf(stderr, "%s: input image too large - larger than %d by %d\n",
  150.                     impXSize(image), impYSize(image));
  151.     exit(1);
  152.     }
  153.  
  154.     /*
  155.      * Create a window and set the graphics mode
  156.      */
  157.     prefsize(impXSize(image), impYSize(image));
  158.     winopen(pname);
  159.     RGBmode();
  160.     gconfig();
  161.  
  162.     /*
  163.      * Draw the image and redraw it when necessary
  164.      */
  165.     if (drawImage(pname, image) < 0)
  166.     exit(1);
  167.     while(1) {
  168.     if (qread(&val) == REDRAW) {
  169.         if (drawImage(pname, image) < 0)
  170.         exit(1);
  171.     }
  172.     }
  173.  
  174.     /*
  175.      * Close the image
  176.      */
  177.     if (impClose(image) < 0) {
  178.         impPerror(pname);
  179.         exit(1);
  180.     }
  181.  
  182.     return 0;
  183. }
  184.  
  185.  
  186. /*
  187.  ==========================================================================
  188.                                 LOCAL FUNCTIONS
  189.  ==========================================================================
  190. */
  191.  
  192.  
  193. /**************************************************************************
  194.  *
  195.  * Function: usage
  196.  *
  197.  * Description: Prints the program usage to stderr.
  198.  *
  199.  * Parameters: 
  200.  *      progName (I) - name of program
  201.  *
  202.  * Return: none
  203.  *
  204.  **************************************************************************/
  205.  
  206. static void usage(char *progName)
  207. {
  208.     fprintf(stderr, "Usage: %s [-o offset] [-c cacheMode] image\n", progName);
  209. }
  210.  
  211.  
  212. /**************************************************************************
  213.  *
  214.  * Function: drawImage
  215.  *
  216.  * Description: Displays the image
  217.  *
  218.  * Parameters: 
  219.  *    progName (I) - program name
  220.  *    image (I) - image to be displayed
  221.  *
  222.  * Return: 0 if no errors. -1 if errors.
  223.  *
  224.  **************************************************************************/
  225.  
  226. static int drawImage(char *progName, IMPImage *image)
  227. {
  228.     register int xsize = impXSize(image);
  229.     register int ysize = impYSize(image);
  230.     register int nchan = impNumChannels(image);
  231.     register int y;
  232.     int off, div = 0;
  233.     short *rs, *gs, *bs;
  234.  
  235.     /*
  236.      * Allocate image row storage
  237.      */
  238.     rs = (short*)malloc(xsize * sizeof(short));
  239.     gs = (short*)malloc(xsize * sizeof(short));
  240.     bs = (short*)malloc(xsize * sizeof(short));
  241.     if (rs == NULL || gs == NULL || bs == NULL) {
  242.     fprintf(stderr, "%s: memory allocation failed\n", progName);
  243.     return -1;
  244.     }
  245.  
  246.     /*
  247.      * If the image is 2 bytes per pixel, determine if
  248.      * scaling is needed and if so calculate the scale
  249.      * factor
  250.      */
  251.     if (impRasterBPP(image) > 1 &&
  252.         (impMaxValue(image) > 255 || impMinValue(image) < 0)) {
  253.     off = impMinValue(image);
  254.     div = (int)(((float)impMaxValue(image) - (float)off) / 255.0 + 0.5);
  255.     }
  256.  
  257.     /*
  258.      * Draw the image
  259.      */
  260.     reshapeviewport();
  261.     viewport(0, xsize-1, 0, ysize-1);
  262.     ortho2(-0.5, (float)xsize - 0.5, -0.5, (float)ysize - 0.5);
  263.     if (nchan < 3) {
  264.         for (y = 0; y < ysize; y++) {
  265.         if (div) {
  266.             if (impReadRow(image, rs, y, 0) < 0) {
  267.             impPerror(progName);
  268.             return -1;
  269.             }
  270.         impSDivRow(rs, rs, div, xsize);
  271.             impPackRow((uchar_t*)rs, rs, xsize);
  272.         } else {
  273.             if (impReadRowB(image, (uchar_t*)rs, y, 0) < 0) {
  274.             impPerror(progName);
  275.             return -1;
  276.             }
  277.         }
  278.         cmov2i(0, y);
  279.         writeRGB(xsize, (uchar_t*)rs, (uchar_t*)rs, (uchar_t*)rs); 
  280.     }
  281.     } else {
  282.         for (y = 0; y < ysize; y++) {
  283.         if (div) {
  284.             if (impReadRow(image, rs, y, 0) < 0) {
  285.             impPerror(progName);
  286.             return -1;
  287.             }
  288.             if (impReadRow(image, gs, y, 1) < 0) {
  289.             impPerror(progName);
  290.             return -1;
  291.             }
  292.             if (impReadRow(image, bs, y, 2) < 0) {
  293.             impPerror(progName);
  294.             return -1;
  295.             }
  296.         impSAddRow(rs, rs, off, xsize);
  297.         impSAddRow(gs, gs, off, xsize);
  298.         impSAddRow(bs, bs, off, xsize);
  299.         impSDivRow(rs, rs, div, xsize);
  300.         impSDivRow(gs, gs, div, xsize);
  301.         impSDivRow(bs, bs, div, xsize);
  302.             impPackRow((uchar_t*)rs, rs, xsize);
  303.             impPackRow((uchar_t*)gs, gs, xsize);
  304.             impPackRow((uchar_t*)bs, bs, xsize);
  305.         } else {
  306.             if (impReadRowB(image, (uchar_t*)rs, y, 0) < 0) {
  307.             impPerror(progName);
  308.             return -1;
  309.             }
  310.             if (impReadRowB(image, (uchar_t*)gs, y, 1) < 0) {
  311.             impPerror(progName);
  312.             return -1;
  313.             }
  314.             if (impReadRowB(image, (uchar_t*)bs, y, 2) < 0) {
  315.             impPerror(progName);
  316.             return -1;
  317.             }
  318.         }
  319.         cmov2i(0, y);
  320.         writeRGB(xsize, (uchar_t*)rs, (uchar_t*)gs, (uchar_t*)bs); 
  321.     }
  322.     }
  323.  
  324.     /*
  325.      * Free the row storage
  326.      */
  327.     free(rs);
  328.     free(gs);
  329.     free(bs);
  330.  
  331.     return 0;
  332. }
  333.